$(function()
{
	var $images = $('#images'), $total = $('#total');
	
	function reload()
	{
		$.ajax(
		{
			'url': WEB+'ajax/gallery/view.php',
			'type': 'GET',
			'data': {'place': PLACE, 'range': RANGE, 'order': ORDER, 'index': INDEX},
			'dataType': 'html',
			'success': function(raw)
			{
				var response = raw.split("\n"), i, image, html;
				
				// Index / Total
				var amounts = response[0].split("\t");
				
				INDEX = parseInt(amounts[0]);
				
				$total.html(amounts[1]);
				
				// Images
				$images.fadeOut(250, function()
				{
					$images.html('');
					
					for(i=1; i<response.length; i++)
					{
						image = response[i].split("\t");
						
						html = '<a href="'+PATH+image[1]+'/" class="image';
						
						if(i === response.length - 1) html += ' right';
						if(image[0] == CURRENT) html += ' current';
						
						html += '"><div><img src="'+WEB+'icon/'+image[1]+'.'+image[2]+'" title="'+image[3]+'"/></div></a>';
						
						$images.append(html);
					}
					
					$images.fadeIn(250);
				});
			}
		});
	}
	
	if(typeof(INDEX) !== 'undefined')
	{
		$('#slide_left').click(function(e)
		{
			e.preventDefault();
			
			INDEX -= 5;
			
			reload();
		});
		
		$('#slide_right').click(function(e)
		{
			e.preventDefault();
			
			INDEX += 5;
			
			reload();
		});
	}
	
	$('#information input').click(function()
	{
		$(this).select();
	});
	
	$('#image').click(function(e)
	{
		e.preventDefault();
		
		var $img = $('<img class="close" src="'+$(this).attr('href')+'" style="display: block;"/>');
		var $body = $('body');
		var $popup = $('<div id="image_popup" class="popup no-border" style="position: absolute;"/>').html($img);
		
		$img.load(function()
		{
			$body.addClass('zoom-out').unbind('click').click(function()
			{
				Popup.close();
			});
			
			Popup.open('image', false, function()
			{				
				$body.unbind('click').removeClass('zoom-out');
			});
		});
		
		$body.prepend($popup);
	});
});